<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="800px"
    @close="close"
  >
    <el-form ref="formRef" label-width="80px" :model="form">
      <el-form-item label="所属组织" prop="AuthCompanys">
        <el-cascader
          v-model="form.AuthCompanys"
          style="width: 100%"
          :options="organizationList"
          :props="companyProps"
          clearable
          @change="getDeptment"
        />
      </el-form-item>
      <el-form-item label="所属部门" prop="AuthDeptments">
        <el-cascader
          v-model="form.AuthDeptments"
          style="width: 100%"
          :options="departmentList"
          :props="companyProps"
          clearable
          @change="getStaffPost"
        />
      </el-form-item>
      <el-form-item label="员工" prop="AuthStaffIDs">
        <el-cascader
          v-model="form.AuthStaffIDs"
          style="width: 100%"
          :options="staffList"
          :props="TaskStaffProps"
          clearable
          @change="aaaaa"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import {
    getCompanySelect,
    getDeptmentSelect,
    GetStaffSelect,
    SaveFlowAuthStaff,
  } from '@/api/common'
  export default defineComponent({
    name: 'SystemsetBusinessAssigner',
    emits: ['fetch-data'],
    setup(props, { emit }) {
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        companyProps: {
          multiple: true,
          checkStrictly: true,
        },
        TaskStaffProps: {
          label: 'StaffName',
          value: 'StaffID',
          multiple: true,
          checkStrictly: true,
        },
        formRef: null,
        form: {
          roles: [],
        },
        rules: {
          AuthCompanys: [
            { required: true, trigger: 'blur', message: '请选择所属组织' },
          ],
          AuthDeptments: [
            { required: true, trigger: 'blur', message: '请选择所属部门' },
          ],
          AuthStaffIDs: [
            { required: true, trigger: 'blur', message: '请先选择员工' },
          ],
        },
        organizationList: [],
        staffList: [],
        departmentList: [],
        title: '',
        dialogFormVisible: false,
      })
      const showEdit = (row) => {
        if (!row) {
          state.title = '指定操作人'
        } else {
          state.title = '指定操作人'
          state.form = Object.assign({}, row)
          console.log(state.form, 'state.form')
        }
        state.form.AuthCompanys = JSON.parse(row.AuthCompanys)
        state.form.AuthDeptments = JSON.parse(row.AuthDeptments)
        state.form.AuthStaffIDs = JSON.parse(row.AuthStaffIDs)
        if (state.form.AuthCompanys.length != 0) {
          getDeptment()
        }
        if (state.form.AuthStaffIDs.length != 0) {
          getStaffPost()
        }

        state.dialogFormVisible = true
      }

      const aaaaa = () => {
        console.log(
          state.form.AuthStaffIDs,
          'AuthStaffIDsAuthStaffIDsAuthStaffIDs'
        )
      }

      const close = () => {
        state['formRef'].resetFields()
        state.form = {}
        state.staffList = []
        state.departmentList = []
        state.dialogFormVisible = false
      }

      const save = () => {
        state['formRef'].validate(async (valid) => {
          if (valid) {
            console.log(state.form, 'state.form')
            const dataMsg = {
              FlowSetID: state.form.FlowSetID,
              AuthStaffIDs: state.form.AuthStaffIDs,
              AuthCompanys: state.form.AuthCompanys,
              AuthDeptments: state.form.AuthDeptments,
            }
            const { msg } = await SaveFlowAuthStaff(dataMsg)
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            emit('fetch-data')
            close()
          }
        })
      }

      // 获取运营组织
      const getOrganization = async () => {
        const { data } = await getCompanySelect()
        console.log(data, '获取组织')
        state.organizationList = data.CompanyOptions
      }
      // 获取部门
      const getDeptment = async () => {
        console.log(state.form.AuthCompanys, '123123')
        const dataMsg = {
          Company: state.form.AuthCompanys,
        }
        const { data } = await getDeptmentSelect(dataMsg)
        console.log(data, '部门')

        state.departmentList = data.DeptmentOptions
      }
      // 获取员工
      const getStaffPost = async () => {
        const dataMsg = {
          Deptment: state.form.AuthDeptments,
          StaffPostID: [],
        }
        const { data } = await GetStaffSelect(dataMsg)
        console.log(data, '员工')

        state.staffList = data.StaffPostOptions
      }

      onMounted(() => {
        getOrganization()
      })

      return {
        ...toRefs(state),
        getStaffPost,
        getDeptment,
        getOrganization,
        aaaaa,
        showEdit,
        close,
        save,
      }
    },
  })
</script>
